package site.wtcc.originalfavor.biz.summary;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.function.Consumer;
import java.util.stream.Stream;

/**
 * 遍历演变
 *
 * @author hexiaodou
 * @date 2022/09/05 14:02:08
 */
public class StreamForEach {
    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>();
        list.add("a");
        list.add("b");
        list.add("c");
        list.add("d");
        list.add("e");
        list.add("f");

        for (int i = 0; i < list.size(); i++) {
            String v1 = list.get(i);
            System.out.print(v1);
        }
        System.out.println();

        for (String v2: list) {
            System.out.print(v2);
        }
        System.out.println();

        Iterator<String> it = list.iterator();
        while (it.hasNext()) {
            System.out.print(it.next());
        }
        for (Iterator<String> it2 = list.iterator(); it2.hasNext(); ) {
            System.out.print(it2.next());
        }
        System.out.println();

        /**
         * @see java.util.function.Predicate
         *  Predicate断言 test
         * @see java.util.function.Supplier
         *  Supplier供应  get
         * @see Consumer
         *  Consumer消费  accept
         * @see java.util.function.Function
         *  Function函数  apply
         */
        Stream<String> stream = list.stream();
        Consumer<String> consumer = new Consumer<String>() {
            @Override
            public void accept(String s) {
                System.out.print(s);
            }
        };
        stream.forEach(consumer);
        System.out.println();
    }
}
